//
//  AYMDiseaseDivViewController.m
//  QuickCure
//
//  Created by guo3920591 on 16/5/6.
//  Copyright © 2016年 ayumi. All rights reserved.
//

#import "AYMDiseaseDivViewController.h"
#import "AYMTumorModel.h"
@interface AYMDiseaseDivViewController ()<UISearchResultsUpdating,UISearchBarDelegate>
/// 疾病细分plist文件数组
@property(nonatomic,strong)NSMutableArray *diseaseDivPlist;
@property(nonatomic,strong)NSMutableArray *searchPlist;
@property(nonatomic,strong)UISearchController *searchcontroller;
@property(nonatomic,strong)NSString *dieaseStringName;
//保存疾病细分名字的数组
@property(nonatomic,strong)NSMutableArray * dieaseArr;
@end

@implementation AYMDiseaseDivViewController

+(void)initialize{
    
    [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitleTextAttributes:    [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
    
    
    
    
}

#pragma mark - 懒加载控件 和plist文件
-(UISearchController *)searchcontroller{
    if (_searchcontroller==nil) {
        _searchcontroller = [[UISearchController alloc] initWithSearchResultsController:nil];
        _searchcontroller.searchResultsUpdater = self;
        _searchcontroller.dimsBackgroundDuringPresentation = NO;
        _searchcontroller.hidesNavigationBarDuringPresentation = NO;
        
        
    }
    return _searchcontroller;
}
-(NSMutableArray *)dieaseArr{
    if(_dieaseArr  == nil){
        
        _dieaseArr = [NSMutableArray array];
    }
    
    return _dieaseArr;
}

-(NSMutableArray *)diseaseDivPlist{
    
    if (_diseaseDivPlist==nil) {
        
        _diseaseDivPlist = [NSMutableArray array];
    }
    
    return _diseaseDivPlist;
}
#pragma mark - 视图生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    self.searchcontroller.searchBar.delegate = self;
    self.searchcontroller.searchBar.barTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"nav"]];
    self.searchcontroller.searchBar.placeholder = @"疾病细分搜索";
    self.navigationItem.titleView = self.searchcontroller.searchBar;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"dieaseDivCell"];
    
    [self loadData];
    
    
}

-(void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    
    if (self.searchcontroller.active) {
        self.searchcontroller.active = NO;
        [self.searchcontroller.searchBar removeFromSuperview];
    }
    
}
#pragma mark - 加载数据
-(void)loadData{
    NSString *urlStr = @"http://iosapi.itcast.cn/searchCI3List.json.php";
    
    NSDictionary *params = @{
                             @"page":@1,
                             @"page_size":@15,
                             @"ci1_id":@1,
                             @"keyword":@""
                             };
    
    [[AYMNetworkTool sharedManager] request:RequestMethodPost urlString:urlStr parameters:params finished:^(id responseObject, NSError *error) {
        
        if (error!=nil) {
            
            Log(@"网络出错");
            return ;
        }
        
        Log(@"%@",responseObject);
        NSArray *arr = responseObject[@"data"];
        
        for (NSDictionary *dict in arr) {
            
            AYMTumorModel *model = [AYMTumorModel yy_modelWithJSON:dict];
            
            [self.dieaseArr addObject:model.ci3_name];
            [self.diseaseDivPlist addObject:model];
        }
        
        
        
        [self.tableView reloadData];
        
    }];
    
}

#pragma mark - tableView数据源
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.searchcontroller.active) {
        return self.searchPlist.count;
    }else{
        
        return self.diseaseDivPlist.count;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"dieaseDivCell" forIndexPath:indexPath];
    if (self.searchcontroller.active) {
        
        
        cell.textLabel.text = self.searchPlist[indexPath.row];
    }else{
        
        AYMTumorModel *model = self.diseaseDivPlist[indexPath.row];
        cell.textLabel.text = model.ci3_name;
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
    
}
#pragma mark - searchUpDater协议
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
    
    NSString *searchString = self.searchcontroller.searchBar.text;
    
    NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@",searchString];
    
    if (self.searchPlist!=nil) {
        
        [self.searchPlist removeAllObjects];
    }
    
    self.searchPlist = [NSMutableArray arrayWithArray:[self.dieaseArr filteredArrayUsingPredicate:preicate]];
    
    [self.tableView reloadData];
    
    
}

#pragma mark  - tableView代理
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    self.dieaseStringName = [tableView cellForRowAtIndexPath:indexPath].textLabel.text;
    
    if (self.diseasedivBlock) {
        
        self.diseasedivBlock(self.dieaseStringName);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark - UISearchBarDelegate
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    
    return YES;
}

-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
    
    return YES;
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    searchBar.showsCancelButton = YES;
    searchBar.tintColor = [UIColor blueColor];
    for(UIView *view in  [[[searchBar subviews] objectAtIndex:0] subviews]) {
        if([view isKindOfClass:[NSClassFromString(@"UINavigationButton") class]]) {
            UIButton * cancel =(UIButton *)view;
            [cancel setTitle:@"取消" forState:UIControlStateNormal];
            cancel.titleLabel.font = [UIFont systemFontOfSize:16];
        }
    }
}


@end
